package com.useeinfo.oa.open.admin;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.data.QueryData;
import com.useeinfo.framework.sugar.data.QueryParam;
import com.useeinfo.framework.sugar.data.TableResultData;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.base.entity.Enterprise;
import com.useeinfo.oa.modules.security.biz.SystemRoleBiz;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.biz.UserRoleEnterpriseBiz;
import com.useeinfo.oa.modules.security.entity.SystemRole;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.entity.UserRoleEnterprise;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-17
 * @createTime: 16:43:28
 */
@Controller
@RequestMapping("/web/admin/enterprise/sytemuser")
public class SystemUserAdminManageAction extends BaseAction {
    @Autowired
    private SystemUserBiz systemUserBiz;
    @Autowired
    private UserRoleEnterpriseBiz userRoleEnterpriseBiz;
    @Autowired
    private SystemRoleBiz systemRoleBiz;
    @Autowired
    private HttpServletRequest request;

    /**
     * 页面跳转接口
     *
     * @return
     */
    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("pages/admin/systemUser/list");
        return mv;
    }

    /**
     * 数据列表查询接口
     *
     * @param pageNo
     * @param pageSize
     * @param userName
     * @return
     */
    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJson(String pageNo, String pageSize, String userName,
            String workNo, String organizationName, String positionName) {

        QueryParam queryParam = new QueryParam(pageNo, pageSize);
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null != enterprise) {
            queryParam.getSqlMap().put("enterpriseIds", enterprise.getId().toString());
        } else {
            queryParam.getSqlMap().put("enterpriseIds", "-1");//企业信息有误 不允许查出数据
        }
        if (!StringConverters.null2String(userName).equals("")) {
            queryParam.getSqlMap().put("userName", userName);
        }
        if (!StringConverters.null2String(workNo).equals("")) {
            queryParam.getSqlMap().put("workNo", workNo);
        }
        if (!StringConverters.null2String(organizationName).equals("")) {
            queryParam.getSqlMap().put("organizationName", organizationName);
        }
        if (!StringConverters.null2String(positionName).equals("")) {
            queryParam.getSqlMap().put("positionName", positionName);
        }
        queryParam.getSqlMap().put("operationRole", "common");
        return systemUserBiz.getSystemUserList(queryParam, SessionKeyUtils.getSessionEnterprise(request));
    }

    /**
     * 编辑页面--跳转接口
     */
    @RequestMapping("/editPage")
    public ModelAndView editPage(String id) {
        ModelAndView modelAndView = new ModelAndView();
        SystemUser systemUser = systemUserBiz.findModel(StringConverters.null2Long(id));
        if (null == systemUser) {
            modelAndView.addObject("systemUser", null);
        } else {
            modelAndView.addObject("systemUser", systemUser.toJSONObjectDetail());
        }
        List<SystemRole> systemRoleList = new ArrayList<>();
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        SystemRole myRole = new SystemRole();
        if (null != enterprise && null != systemUser) {
            List<UserRoleEnterprise> userRoleEnterprises = userRoleEnterpriseBiz.getUserRoleEnterpriseList(systemUser, null, enterprise);
            if (CollectionUtils.isNotEmpty(userRoleEnterprises)) {
                myRole = userRoleEnterprises.get(0).getSystemRole();
            }
        }
        if (null != enterprise) {
            String roleHql = "select obj from SystemRole obj where obj.enabled is false and obj.enterprise.id = " + enterprise.getId();
            systemRoleList = systemRoleBiz.hqlExcute(roleHql);
            if(!systemRoleList.contains(myRole)&&null!=myRole.getId()){
                systemRoleList.add(myRole);
            }
        }
        modelAndView.addObject("systemRoleList", systemRoleList);
        modelAndView.addObject("myRole", myRole);
        modelAndView.setViewName("pages/admin/systemUser/edit");
        return modelAndView;
    }

    /**
     * 企业用户编辑保存
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public ResultDto saveOrUpdate(String id, String password, String roleId) {
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        if (null == enterprise) {
            return ResultUtil.error("当前登录用户企业信息有误");
        }

        try {
            return systemUserBiz.saveEdit(enterprise, id, password, roleId);
        } catch (Exception e) {
            logger.info(e.getMessage());
            return ResultUtil.error();
        }

    }

    /**
     * 用户重置密码接口
     * @param id
     * @return
     */
    @RequestMapping("/resetPassword")
    @ResponseBody
    public JSONObject resetPassword(String id){
        return systemUserBiz.resetPassword(id);
    }
}
